
import config from '@/request/baseUrl.js';

function request({method, url, data}, toast = true) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: config.baseUrl + url,
			data,
			timeout: 1000 * 60, // 超时时间
			method,
			success(res) {
				switch (res.statusCode) {
					case 200:
						if (res.data.code == 200) {
							resolve(res.data)
						} else {
							resolve(res.data)
							if (toast) {
								uni.showToast({
									title: res.data.msg || '服务器错误',
									icon: 'none',
									mask: true,
									duration: 1500
								})
							}
						}
					break;
					default:
						if (toast) {
							uni.showToast({
								title: '服务器错误',
								duration: 1500,
								icon: 'error'
							})
						}
					break;
				}
			},
			fail() {
				if (toast) {
					uni.showToast({
						title: '网络错误',
						duration: 1500,
						icon: 'none'
					})
				}
				resolve({code: 500, msg: '网络错误'})
			}
		})
	})
}

export default request;
